package com.idna.wsconsumer.utils;

import java.io.*;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.impl.Log4JLogger;
import org.apache.commons.logging.impl.SimpleLog;

/**
 * A utility class providing helper functions
 * 
 * @author kan.sun
 *
 */
public class ApplicationUtilImpl implements ApplicationUtil
{
	public static final String RESOURCES = "src/main/resources/";
	public static final String PROP_PATH = new File(RESOURCES+"properties/services.properties").getAbsolutePath();
	public static final Log LOGGER = new SimpleLog("logger");
	/* (non-Javadoc)
	 * @see net.icdpublishing.wsconsumer.utils.ApplicationUtil#readFile(java.lang.String)
	 */
	public String readFile(String fileName) throws FileNotFoundException, IOException
	{
		//if(LOGGER.isDebugEnabled())
			LOGGER.debug(fileName);
		String path = new File(fileName).getAbsolutePath();
		BufferedReader reader = new BufferedReader(new FileReader(path));
		String line = "";
		String fileContents = "";
		while((line=reader.readLine())!=null)
		{
			fileContents += line;
		}
		return fileContents.trim();
	}

	/* (non-Javadoc)
	 * @see net.icdpublishing.wsconsumer.utils.ApplicationUtil#loadProperty(java.lang.String)
	 */
	public Properties loadProperty(String path) throws FileNotFoundException, IOException
	{
		Properties prop = new Properties();
		if (path.toLowerCase().endsWith(".properties"))
			prop.load(new FileInputStream(path));
		else if (path.toLowerCase().endsWith(".xml"))
			prop.loadFromXML(new FileInputStream(path));
		return prop;
	}
	public static void main(String[] args)
	{
		ApplicationUtilImpl utils = new ApplicationUtilImpl();
		try
		{
			System.out.println(utils.readFile(RESOURCES + "xmlrequests/ProveIDXMLRequest.xml"));
		}
		catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
